<template>
  <div class="payManage page-main">
    <CustomForm :formInline="formInline" @onSubmit="onSubmit" @resetTable="resetTable" :btnOnLine="true">
      <el-form-item label="申请人">
        <el-input v-model="formInline.applicant" placeholder="输入申请人"></el-input>
      </el-form-item>
      <el-form-item label="公司名称">
        <el-input v-model="formInline.company" placeholder="输入公司名称"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="formInline.status" placeholder="选择状态" @change="initial">
          <el-option label="全部" value="0"></el-option>
          <el-option label="未审批" value="1"></el-option>
          <el-option label="已审批" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="批次号">
        <el-input v-model="formInline.batch_number" placeholder="输入批次号"></el-input>
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker
          v-model="filterapply_time"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
    </CustomForm>

    <CustomDescriptions :data="descriptionsData" :styleBox="{ width: '100%' }"></CustomDescriptions>
    <div class="main">
      <el-table
        border
        v-loading="tableLoading"
        ref="multipleTable"
        :data="tableData"
        height="600"
        tooltip-effect="dark"
        style="width: 100%;"
      >
        <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
        <el-table-column prop="applicant" label="申请人" align="center"></el-table-column>
        <el-table-column prop="goods" :show-overflow-tooltip="true" label="货物名称" align="center"></el-table-column>

        <el-table-column prop="company" :show-overflow-tooltip="true" label="公司名称" align="center"></el-table-column>
        <el-table-column
          prop="batch_number"
          :show-overflow-tooltip="true"
          label="批次号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="bill_number"
          :show-overflow-tooltip="true"
          label="运单数量"
          align="center"
          width="80"
        ></el-table-column>
        <el-table-column prop="money" :show-overflow-tooltip="true" label="总金额(元)" align="center"></el-table-column>
        <el-table-column prop="weight" :show-overflow-tooltip="true" label="总重量(吨)" align="center"></el-table-column>
        <el-table-column
          prop="apply_time"
          :show-overflow-tooltip="true"
          label="申请时间"
          align="center"
        ></el-table-column>
        <el-table-column prop="weight" :show-overflow-tooltip="true" label="状态" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 0" style="color: #F56C6C">未审批</span>
            <span v-if="scope.row.status == 1" style="color: #67C23A">已审批</span>
            <span v-if="scope.row.status == 2" style="color: #324157">审批驳回</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="checkDetail(scope.row)">详情</el-button>
            <el-button
              v-if="scope.row.status == 2 || scope.row.status == 0"
              size="mini"
              type="danger"
              @click="cancleApprove(scope.row)"
            >撤销</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="$pageSizeList"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>

      <el-dialog
        class="dialog"
        title="审批详情"
        :close-on-click-modal="true"
        :visible.sync="dialogVisible"
        top="5vh"
        width="95%"
      >
        <payManageDetail v-if="dialogVisible" :form="detailForm" @closeDialog="closeDialog" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getApproveDingList, revokeApproval } from "@/api/moneyManagment";
import payManageDetail from "../components/payManageDetail.vue";
export default {
  name: "payManage",
  components: {
    payManageDetail
  },
  data() {
    return {
      currentPage: 1,
      total: 0,
      pageSize: 10,
      tableData: [],
      tableLoading: false,
      formInline: {
        company: "", //公司名称
        status: "0", //1未审核2审核通过
        apply_time: "", //申请时间
        applicant: "", //申请人
        batch_number: "" //批次号
      },
      filterapply_time: "",
      dialogVisible: false,
      detailForm: {},
      descriptionsData: [

        {
          label: "总金额",
          count: 0,
          unit: "(元)"
        },
        {
          label: "总重量",
          count: 0,
          unit: "(吨)"
        },
        {
          label: "总单数",
          count: 0,
          unit: "(单)"
        },
      ],
    };
  },
  mounted() {
    this.initial();
  },
  methods: {
    closeDialog() {
      this.dialogVisible = false;
      this.initial();
    },
    async initial() {
      this.tableLoading = true;
      let result = await getApproveDingList({
        ...this.formInline,
        pageSize: this.pageSize,
        page: this.currentPage
      });
      this.tableLoading = false;
      if (result.code == 200) {
        this.tableData = result.data.data;
        this.total = result.data.total;
        this.descriptionsData[0].count = result.money;
        this.descriptionsData[1].count = result.weight;
        this.descriptionsData[2].count = result.bill_number;
      }
    },
    onSubmit() {
      this.currentPage = 1;
      this.formInline.apply_time = (this.filterapply_time
        ? this.filterapply_time
        : []
      ).join(" - ");
      this.initial();
    },
    resetTable() {
      this.filterapply_time = "";
      this.formInline = {
        company: "", //公司名称
        status: "0", //1未审核2审核通过
        apply_time: "", //申请时间
        applicant: "", //申请人
        batch_number: "" //批次号
      };

      this.initial();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.page = 1;
      this.initial();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.initial();
    },
    checkDetail(item) {
      this.detailForm = item;
      this.dialogVisible = true;
    },
    async cancleApprove(row) {
      this.$confirm("确定要撤销审批吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          revokeApproval({
            batch_number: row.batch_number
          }).then(res => {
            if (res.code == 200) {
              this.$message.success("撤销成功");
              this.initial();
            }
          });
        })
        .catch(e => {});
    }
  }
};
</script>

<style lang="scss" scoped>
.payManage {
  padding: 10px;
  background: #fff;
  ::v-deep .el-dialog {
    overflow: hidden;

    .el-dialog__body {
      height: calc(88vh - 40px);
      overflow: auto;
    }
  }
}
</style>
